<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <div class="tatletitle">
      <el-row :gutter="20">
        <!--业务类别数据-->
        <el-col :span="5" :xs="24">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              ref="tree"
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              @node-click="handleNodeClick"
            >
              <span slot-scope="{ node, data }" class="custom-tree-node">
                <span>{{ node.label }}</span>
                <span class="childrenNum">{{ data.childrenNum }}</span>
              </span>
            </el-tree>
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="19" :xs="24">
          <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="案例名称" prop="title">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="开始服务起始年度" prop="beginYear" label-width="130px">
              <el-date-picker
                v-model="beginYear"
                clearable
                size="small"
                style="width: 200px"
                type="year"
                placeholder="选择年度"
                @change="getbeginYear"
              />
            </el-form-item>
            <el-form-item label="结束服务起始年度" prop="endYear" label-width="130px">
              <el-date-picker
                v-model="endYear"
                clearable
                size="small"
                style="width: 200px"
                type="year"
                placeholder="选择年度"
                @change="getendYear"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['library:case:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                v-hasPermi="['library:case:remove']"
                type="primary"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
              >删除</el-button>
            </el-col>
          </el-row>
          <el-table
            stripe
            v-loading="loading"
            :data="dataList"
            @selection-change="handleSelectionChange"
            @sort-change="tableSortChange"
            :default-sort="{prop: 'year', order: 'descending'}"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="案例名称"
              align="center"
              prop="title"
              :sort-orders="['ascending', 'descending']"
              sortable="custom"
            />
            <el-table-column label="业务类别" align="center" prop="businessCategorys">
              <template slot-scope="{row}">
                <div v-for="(item,index) in row.businessCategorys" :key="index">{{ item.menuName }}</div>
              </template>
            </el-table-column>
            <el-table-column
              label="案例所在地区"
              align="center"
              prop="city"
              :sort-orders="['ascending', 'descending']"
              sortable="custom"
            />
            <el-table-column
              label="服务起始年度"
              align="center"
              prop="year"
              :sort-orders="['ascending', 'descending']"
              sortable="custom"
            />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="handleView(scope.row)"
                  icon="el-icon-view"
                >查看详情</el-button>
                <el-button
                  v-hasPermi="['library:case:edit']"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >编辑</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>

      <!-- 添加或修改归档模板对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="720px">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="案例名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入" :disabled="isDisabled" />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <div class="grid-content bg-purple">
                <el-form-item label="案例所在地区" prop="citys">
                  <el-cascader
                    v-model="form.citys"
                    :options="provinceCityOption"
                    :show-all-levels="true"
                    :props="{ value: 'id' }"
                    filterable
                    style="width:100%"
                    :disabled="isDisabled"
                  />
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务起始年度" prop="year">
                <el-date-picker
                  v-model="year"
                  clearable
                  size="small"
                  type="year"
                  placeholder="选择年度"
                  :disabled="isDisabled"
                  @change="getYear"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="业务类别" prop="businessCategoryGuids">
            <treeselect
              v-model="form.businessCategoryGuids"
              :options="deptOptions"
              placeholder="请选择业务类别"
              :disable-branch-nodes="true"
              :multiple="true"
              :disabled="isDisabled"
            />
          </el-form-item>
          <el-form-item label="案例概要说明" prop="content">
            <el-input
              v-model="form.content"
              placeholder="案例概要说明"
              type="textarea"
              :rows="2"
              autosize
              :disabled="isDisabled"
            />
          </el-form-item>
          <el-form-item label="附件" prop="attachmentId">
            <vue-uploader
              v-if="open"
              :single-type="taskSingle"
              :file-list="form.attachments"
              :upload-readonly="isDisabled"
              @changeFileData="changeFile"
              @removeFileList="removeFileList"
              :pathName="'TJECdocuments/业务标准库/典型案例'"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              placeholder="备注"
              type="textarea"
              :rows="2"
              :disabled="isDisabled"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import aTitle from '@/components/Title';
import {
  caseSelectList,
  getCaseDetail,
  addLibraryCase,
  deleteLibraryCase,
  updatelibraryCase
} from '@/api/library/case.js';
import { provinceCityTwo } from '@/api/provinceCity';
import { libraryTreeListMore } from '@/api/library/manual.js';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import vueUploader from '@/components/Uploader';
import axios from 'axios';
export default {
  name: 'typicalCase',
  components: {
    Treeselect,
    aTitle,
    vueUploader
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      isDisabled: true,
      taskSingle: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortBy: undefined,
        order: undefined,
        title: undefined,
        starYear: undefined,
        endYear: undefined,
        businessCategoryId: undefined
      },
      year: undefined,
      beginYear: undefined,
      endYear: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        // attachmentId: [
        //   { required: true, message: "附件不能为空", trigger: "blur" }
        // ],
        businessCategoryGuids: [
          { required: true, message: "业务类别不能为空", trigger: "blur" }
        ]
      },
      // 省市
      provinceCityOption: []
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName (val) {
      this.$refs.tree.filter(val)
    }
  },
  created () {
    this.getList()
    this.getTreeselect()
    provinceCityTwo().then(res => {
      this.provinceCityOption = res.data
    })
  },
  methods: {
    getYear (val) {
      console.log(val)
      this.form.year = new Date(val).getFullYear()
    },
    getendYear (val) {
      this.queryParams.endYear = new Date(val).getFullYear()
    },
    getbeginYear (val) {
      this.queryParams.starYear = new Date(val).getFullYear()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel () {
      this.open = false
    },
    resetQuery () {
      this.queryParams.title = undefined
      this.beginYear = undefined
      this.endYear = undefined
      this.queryParams.pageNum = 1
      this.queryParams.starYear = undefined
      this.queryParams.endYear = undefined
      this.queryParams.businessCategoryId = undefined
      this.getList()
    },
    // 表单重置
    reset () {
      this.year = undefined
      this.form = {
        guid: undefined,
        attachments: [],
        attachmentIds: []
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.isDisabled = false
      this.title = '添加典型案例';
    },
    handleView (row) {
      this.isDisabled = true
      getCaseDetail({ guid: row.guid }).then(response => {
        this.form = response.data
        this.year = response.data.date
        this.open = true
        this.title = '案例典型详情';
      })
    },
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务标准库/典型案例.xlsx`
      )}&name=${encodeURIComponent("典型案例.xlsx")}`;
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.isDisabled = false
      getCaseDetail({ guid: row.guid }).then(response => {
        this.form = response.data
        this.year = response.data.date
        this.open = true
        this.title = '修改典型案例';
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.guid !== undefined) {
            updatelibraryCase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
                this.getTreeselect()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addLibraryCase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
                this.getTreeselect()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids
      this.$confirm(
        '是否确认删除所有业务类别下的此典型案例? 如只删除该业务类别，请通过“修改”功能操作。',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function () {
          return deleteLibraryCase(ids)
        })
        .then(() => {
          this.getList()
          this.getTreeselect()
          this.msgSuccess('删除成功')
        })
        .catch(function () { })
    },
    /** 查询归档模板列表 */
    getList () {
      this.loading = true
      caseSelectList(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect () {
      libraryTreeListMore('TYPICAL_CASES').then(response => {
        this.deptOptions = JSON.parse(
          JSON.stringify(response.data).replace(/isDisabled/g, 'checkbox')
        )
      })
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true
      if (data.label != null) {
        return data.label.indexOf(value) !== -1
      }
    },
    // 节点单击事件
    handleNodeClick (data) {
      this.queryParams.businessCategoryId = data.id
      this.getList()
    },
    // 附件操作
    changeFile (id, name) {

      this.form.attachmentIds.push(id)

      // this.$set(this.form.attachments, this.form.attachments.length, { fileName: name, guid: id })

      // console.log(this.form)

    },
    removeFileList (id) {
      this.form.attachmentIds = this.form.attachmentIds.filter(
        item => item != id
      )
      this.form.attachments = this.form.attachments.filter(
        item => item.guid != id
      )
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
}
</script>
<style lang="scss" scoped>
.tatletitle {
  margin: 0 20px;
}
/deep/.el-input.is-disabled {
  .el-input__prefix {
    display: none;
  }
  .el-input__suffix {
    display: none;
  }
}

/deep/.el-input.is-disabled .el-input__inner {
  background-color: #fff;
  border-color: #fff;
  color: #606266;
  cursor: auto;
  padding-left: 15px;
}
/deep/.el-input.is-disabled .el-input__inner:hover {
  background-color: #fff;
  border-color: #fff;
  color: #606266;
  cursor: auto;
}
/deep/.el-input.is-disabled .el-input__inner::-webkit-input-placeholder {
  color: #fff;
}
/deep/.el-input.is-disabled .el-input__inner::-moz-placeholder {
  color: #fff;
}
/deep/.el-input.is-disabled .el-input__inner::-ms-input-placeholder {
  color: #fff;
}

/deep/ .el-textarea.is-disabled .el-textarea__inner {
  background-color: #fff;
  border-color: #fff;
  color: #606266;
  resize: none;
  cursor: auto;
}
/deep/.el-textarea.is-disabled .el-textarea__inner::-webkit-input-placeholder {
  color: #fff;
}
/deep/.el-textarea.is-disabled .el-textarea__inner::-moz-placeholder {
  color: #fff;
}
/deep/.el-textarea.is-disabled .el-textarea__inner::-ms-input-placeholder {
  color: #fff;
}

/deep/.vue-treeselect--disabled .vue-treeselect__control {
  background-color: #fff;
  border-color: #fff;
  color: #606266;
}
/deep/.vue-treeselect--disabled .vue-treeselect__control-arrow-container {
  display: none;
}
</style>
